package com.leo.blog.login;

import com.leo.blog.dto.BlogerExecution;
import com.leo.blog.entity.Bloger;
import com.leo.blog.service.BlogerService;
import com.leo.blog.util.CacheModel;
import com.leo.blog.util.CacheUtils;
import com.leo.blog.util.SpringContextUtils;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Classname SecurityUtils
 * @Description TODO
 * @Date 2023/4/13 11:24
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class SecurityUtils {

    public static SelfUserEntity getSysUser(){
       SelfUserEntity selfUserEntity=(SelfUserEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
       return selfUserEntity;
    }

    public static Bloger getBloger(){
        SelfUserEntity selfUserEntity=getSysUser();
        Object obj=CacheUtils.getObject(selfUserEntity.getUserId());
        if(obj==null){
            BlogerService blogerService= SpringContextUtils.getBean(BlogerService.class);
            BlogerExecution execution=blogerService.getBlogerById(Integer.valueOf(selfUserEntity.getUserId()));
            CacheUtils.putExpireTime(selfUserEntity.getUserId(),execution.getBloger(),3000);
            return execution.getBloger();
        }
        CacheModel cacheModel=(CacheModel) obj;
        Bloger bloger=(Bloger)cacheModel.getValue();
        return bloger;
    }
}
